package com.lcy.routine.case2.client;

import com.lcy.routine.case2.request.BaseRequest;
import com.lcy.routine.case2.request.GoodsDetailRequest;
import com.lcy.routine.case2.request.GoodsListRequest;
import com.lcy.routine.case2.request.OrderDetailRequest;
import com.lcy.routine.case2.response.BaseResponse;
import com.lcy.routine.case2.response.GoodsDetailResponse;
import com.lcy.routine.case2.response.GoodsListResponse;
import com.lcy.routine.case2.response.OrderDetailResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class jdClient {

    private String postRequest(String param){
        return HttpUtil.createPost("https://api.m.jd.com/api").body(param).execute().body();
    }

    public <T extends BaseResponse> T execute(BaseRequest<T> request){
        String result = postRequest(JSONUtil.toJsonStr(request));
        return JSONUtil.toBean(result, request.getResponseType());
    }

    public static void main(String[] args) {
        jdClient client = new jdClient();
        GoodsListRequest request = new GoodsListRequest();
        request.setGoodsIds(List.of("10001"));

        GoodsListResponse goodsListResponse = jdClient.execute(request);

        GoodsDetailRequest goodsDetailRequest = new GoodsDetailRequest();
        GoodsDetailResponse goodsDetailResponse = jdClient.execute(goodsDetailRequest);

        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId("10001");
        OrderDetailResponse orderDetailResponse = jdClient.execute(orderDetailRequest);

    }

}
